﻿
var UploadDlg_AppID = -1;
var UPLOAD_PAGE_URL = '';

//this function runs on document.ready()
$(function () {
    //create an add candidate popup dlg
    $("#UploadDocumentPopup").dialog({
        modal: true,
        resizable: false,
        width: 400,
        height: 300,
        position: ['center', 'center'],
        autoOpen: false,
        buttons:
        [
            {
                text: "Upload",
                click: OnUploadClicked
            },
            {
                text: "Cancel",
                click: function () { $(this).dialog("close"); }
            }
        ],
        title: "Upload Document",
        open: OnUploadDlgOpen
    });

    var options = {
        success: OnFileUploadDone, //function in CandidatePage.js
        iframe: 'true',
        dataType: 'json',
        url: UPLOAD_PAGE_URL,
        error: function () { alert('error'); },
        clearForm: true,
        resetForm: true,
        beforeSubmit: PrepareFileUpload
    };

    // bind to the form's submit event 
    $('#UploadForm').submit(function () {
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options);

        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false;
    });

    //make the heard about box a jquery ui combobox
    $("#UploadPopup_Category").combobox({
        valueselect: function (event, ui) { $("#UploadDlgCatID").val(ui.item.itemID); },
        source: []
    });
    //make the heard about a true combo box, not editable
    $("#UploadPopup_CategoryCombobox").find("input").attr('readonly', 'readonly');
    $("#UploadPopup_CategoryCombobox").find("input").attr('name', 'CatName');
});

function OnFileUploadDone(data) {
    if (typeof OnFileUploadDoneOverride == 'function') {
        OnFileUploadDoneOverride(data);
        location.reload(true);//refresh page for now
    }
}

function OnUploadDlgOpen() {

    //reset any errors or messages
    $("#UploadDocumentPopup").find("input").val('');
    $("#UploadDocumentPopup").find("textarea").val('');
    $("#UploadDocumentPopup").find(".ui-state-error").removeClass("ui-state-error");
    $("#UploadPopup_Message").addClass("ui-helper-hidden");
    $("#UploadPopup_Error").addClass("ui-helper-hidden");

    //get heard abouts from the database
    PageMethods.GetAllCategories_Json(
        function (json) {
            //send this data to the combobox
            $("#UploadPopup_Category").combobox("option", "source", eval(json));
        },
        function () {
            //TODO: show error message
            $("#UploadPopup_CategoryCombobox").addClass("ui-state-error");

            //set empty data
            $("#UploadPopup_Category").combobox("option", "source", []);
        }
    );
}

function PrepareFileUpload() {

    $("#UploadDlgAppID").val(UploadDlg_AppID);
}

function OnUploadClicked() {

    //clear out any messages
    $("#UploadPopup_Message").addClass("ui-helper-hidden");
    $("#UploadPopup_Error").addClass("ui-helper-hidden");

    //get all the data
    var file = $("#UploadDlgFile");
    var category = $("#UploadDlgCatID");
    var catContainer = $("#UploadPopup_CategoryCombobox").find("input").first();

    //validate the data
    var bValid = true;

    //name can't be empty
    if (file.val().length == 0) {
        file.addClass("ui-state-error");
        bValid = false;
    }
    else {
        file.removeClass("ui-state-error");
    }

    //heard about can't be empty
    if (category.val().length == 0) {
        catContainer.addClass("ui-state-error");
        bValid = false;
    }
    else {
        catContainer.removeClass("ui-state-error");
    }

    if (bValid) {
        $('#UploadForm').submit();
    }
    else {
        //not valid, show message
        $("#UploadPopup_Message").removeClass("ui-helper-hidden");
    }
}
